package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.ParseException
import java.text.SimpleDateFormat;
import java.time.Instant

import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import cn.com.kh.hst.ThreeDesUtil
import cn.com.kh.KHUtil;
import cn.com.kh.getProperty;


/**
 *
 * @Title: aipHSTLabel_Customers2
 * @Description:华盛证券 标签客户查询接口
 * @author: xrm
 * @date 2022-11-24 15:52:00
 *
 */

public class aipHSTLabel_Customers2 implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(aipHSTLabel_Customers2.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);
		
		String inData = "";
        String outData = "";
        String totalData = "";
        String dataSourceID = "dataSource";

      
        try {
			
			List date_formatlist = eliteBean.chaxun_sql("select date_format(curdate(),'%w') AS week,DATE_FORMAT(NOW(), '%d') AS week2 from dual", "dataSource");
            def date_format = date_formatlist.get(0).get("week");//周几
            def date_format2 = date_formatlist.get(0).get("week2");//每月几号

            List hst_Activitylist = eliteBean.chaxun_sql("select tab_activity_name,frequency from hst_Activity where tab_task_type='2' and durationdate<=now() and durationenddate>=now() and tab_activity_state!='3'", "dataSource");//查询批次
			log.debug("hst_Activitylist:" + hst_Activitylist.size);
			if (hst_Activitylist.size() > 0) {
	                     int arrInt = hst_Activitylist.size();
	                     for (int i = 0; i < arrInt; i++) {
							String tagname = hst_Activitylist.get(i).get("tab_activity_name");
							String frequency = hst_Activitylist.get(i).get("frequency");
							
							if(frequency=="1"){//频次每日
							  String sign_time=Instant.now().getEpochSecond();
	                          log.debug("解析信息-" +  "sign_time: " + sign_time);
							     String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
	                             log.debug("injson:" + injson);
								 
								 totalData = Querypromocode(eliteBean, injson,dataSourceID);
				                 //解析SSO返回信息
				                 //JSONObject jsonObject = new JSONObject(totalData);
				                 //log.debug("jsonObject:"+jsonObject);
							}
							if(frequency=="2"&&date_format=="1"){//频次每周
							  String sign_time=Instant.now().getEpochSecond();
	                          log.debug("解析信息-" +  "sign_time: " + sign_time);
							     String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
	                             log.debug("injson:" + injson);
								 
								 totalData = Querypromocode(eliteBean, injson,dataSourceID);
				                 //解析SSO返回信息
				                 //JSONObject jsonObject = new JSONObject(totalData);
				                 //log.debug("jsonObject:"+jsonObject);
							}
							if(frequency=="3"&&date_format2=="1"){//批次每月
							  String sign_time=Instant.now().getEpochSecond();
	                          log.debug("解析信息-" +  "sign_time: " + sign_time);
							     String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
	                             log.debug("injson:" + injson);
								 
								 totalData = Querypromocode(eliteBean, injson,dataSourceID);
				                 //解析SSO返回信息
				                 //JSONObject jsonObject = new JSONObject(totalData);
				                 //log.debug("jsonObject:"+jsonObject);
							
							}
						 }
						 
			}
			

            outData = "{\"result\":\"1\",\"message\":\"处理成功\",\"data\":[]}";


        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            outData = "{\"result\":\"-1\",\"message\":\"处理失败(" + e.getMessage() + ")\",\"data\":[]}";
        }

        return outData;
    }

    //标签客户查询接口 URI:	/mp-dabs/queryTagMember
    public static String Querypromocode(EliteBean eliteBean , String injson, String dataSourceID) {

        long startTime = System.currentTimeMillis();
        log.debug("处理开始时间:" + startTime);

        long endTime = System.currentTimeMillis();

        String reJson = null;
		String tagname="";
        try {
			  String doURL ="";
			  //获取配置参数
              String sysurl = getProperty.get("crmurl");//配置本地接口地址
  
             //华盛 服务顾问列表查询
              doURL = sysurl+"aipHSTLabel_Customers";

           // String doURL = "http://172.19.20.35:8080/EliteGeneralHttpTask/GeneralService/aipHSTLabel_Customers";
            log.debug("doURL:" + doURL);
            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/x-www-form-urlencoded");
            def getResult = Util.doHttpPostUseHead(doURL, injson, "UTF-8", 10000, 0, headMap);
            log.debug("getResult:" + getResult);
        } catch (Exception e) {
		    log.debug("ddddddddddd");
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
           reJson = "{\"result\":\"-1\",\"message\":\"接口异常\",\\\"lastid\\\":\"0\"}";
        }
        return reJson;
    }

    // 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;

		try {
			Rev_Value = String.valueOf(inJson.getBoolean(getKey));
		} catch (Exception e6) {
			try {
				Rev_Value = inJson.getString(getKey);
			} catch (Exception e5) {
				try {
					Rev_Value = String.valueOf(inJson.getInt(getKey));
				} catch (Exception e4) {
					try {
						Rev_Value = String.valueOf(inJson.getLong(getKey));
					} catch (Exception e11) {
						try {
							Rev_Value = String.valueOf(inJson.getDouble(getKey));
						} catch (Exception e1) {
							try {
								Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
							} catch (Exception e2) {
								try {
									Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
								} catch (Exception e3) {
									Rev_Value = "";
								}
							}
						}
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			}
		}
		return Rev_Value;
	}


    // 非空校验isNull
    public static boolean isNull(String value) {

        boolean reFlag = true;
        if (value == "" || value.length() <= 0) {
            reFlag = true;
        } else {
            reFlag = false;
        }
        log.debug(reFlag);
        return reFlag;
    }

    // 非空校验isNullMSG
    public static String isNullMSG(String key, String value) {

        String reResult = "";
        if (value == "" || value.length() <= 0) {
            reResult = key + "必填字段;";
        }
        return reResult;
    }

}
